package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.constant.ExcelHeadConstant;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.CzService;
/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月15日 上午9:34:42
 * @功能：充值结果界面
 */
@Controller
@RequestMapping("/cz")
public class CzController  extends JsonController {
	@Autowired
	private CzService CzService;
	/**
	 * 
	 * @param request
	 * @param response 查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryCzList")
	public ModelAndView queryCzList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String minczje =RequestUtils.getStringParameter(request, "minczje");
		String maxczje =RequestUtils.getStringParameter(request, "maxczje");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String czsj = RequestUtils.getStringParameter(request, "czsj");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String czState= RequestUtils.getStringParameter(request, "czState");
		String gyh= RequestUtils.getStringParameter(request, "gyh");
		String tjr= RequestUtils.getStringParameter(request, "tjr");
		String ddh= RequestUtils.getStringParameter(request, "ddh").toUpperCase();
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("minczje", minczje);
		params.put("maxczje", maxczje);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("czsj", czsj);
		params.put("czState", czState);
		params.put("sjhm", sjhm);
		params.put("gyh", gyh);
		params.put("tjr", tjr);
		params.put("ddh", ddh);
		
		
		List<Map<String, Object>> list = CzService.queryCzList(params);
		int count = CzService.getCzCount(params);
		String czjeSum = CzService.geCzjeSum(params);
		result.put("data", list);	
		result.put("total", count);
		result.put("czjeSum", czjeSum);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("GYH", "合计:");//key是字段名
			map.put("CZJE", czjeSum);//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@RequestMapping("exportCzExcelMethod")
	public ModelAndView exportCzExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		JSONObject params = new JSONObject();
		
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String minczje =RequestUtils.getStringParameter(request, "minczje");
		String maxczje =RequestUtils.getStringParameter(request, "maxczje");
		String startDate = RequestUtils.getStringParameter(request, "start_date");
		String endDate = RequestUtils.getStringParameter(request, "end_date");
		String czsj = RequestUtils.getStringParameter(request, "czsj");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String czState= RequestUtils.getStringParameter(request, "czState");
		String gyh= RequestUtils.getStringParameter(request, "gyh");
		String tjr= RequestUtils.getStringParameter(request, "tjr");
		String ddh= RequestUtils.getStringParameter(request, "ddh").toUpperCase();
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("minczje", minczje);
		params.put("maxczje", maxczje);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("czsj", czsj);
		params.put("czState", czState);
		params.put("sjhm", sjhm);
		params.put("gyh", gyh);
		params.put("tjr", tjr);
		params.put("ddh", ddh);
		
		
		// 查询数据
		List<Map<String, Object>> list = CzService.queryCzList(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getCzExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"充值明细");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "充值明细.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}
